package com.example.cybz.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cybz.acl.mapper.RoleMapper;
import com.example.cybz.acl.service.AdminRoleService;
import com.example.cybz.acl.service.RoleService;
import com.example.cybz.model.acl.AdminRole;
import com.example.cybz.model.acl.Role;
import com.example.cybz.vo.acl.RoleQueryVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Log4j2
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    private final AdminRoleService adminRoleService;

    /**
     * 分页查询角色信息
     * @param roleQueryVo 角色查询条件封装对象
     * @param rolePage 分页参数对象
     * @return 返回分页后的角色信息
     */
    @Override
    public Page<Role> selectRolePage(RoleQueryVo roleQueryVo, Page<Role> rolePage) {
        // 获取查询条件中的角色名称
        String roleName = roleQueryVo.getRoleName();
        LambdaQueryChainWrapper<Role> roleLambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(getBaseMapper());
        
        // 如果角色名称不为空，则添加模糊查询条件
        if (!StringUtils.isEmpty(roleName)){
            log.info("roleName:" + roleName);
            roleLambdaQueryChainWrapper.like(Role::getRoleName, roleName);
        }

        // 执行分页查询
        Page<Role> page = roleLambdaQueryChainWrapper.page(rolePage);
        // 记录查询成功日志
        log.info("查询成功");
        return page;
    }

    /**
     * 根据用户ID查询角色信息
     * @param admin 用户ID
     * @return 返回包含全部角色列表和用户分配角色的Map
     */
    @Override
    public Map<String, Object> selectRoleByUserId(Long admin) {

        Map<String, Object> result = new HashMap<>();

        // 查询用户拥有的角色信息
        List<AdminRole> adminRoles = adminRoleService.selectRoleByUserId(admin);

        // 查询所有角色信息
        List<Role> allRolesList = baseMapper.selectList(null);

        // 提取用户已分配的角色ID列表
        List<Long> roleIdList = adminRoles.stream().map(AdminRole::getRoleId).collect(Collectors.toList());

        // 创建用于存储用户实际角色的列表
        ArrayList<Role> roleArrayList = new ArrayList<>();

        // 过滤出用户拥有的角色
        for(Role role: allRolesList){
            if (roleIdList.contains(role.getId())){
                roleArrayList.add(role);
            }
        }
        
        // 将结果封装到Map中
        result.put("allRolesList", allRolesList);
        result.put("assignRoles", roleArrayList);
        return result;
    }

    /**
     * 保存用户角色关系
     * @param adminId 用户ID
     * @param roleIdList 角色ID列表
     * @return 返回操作是否成功
     */
    @Override
    public boolean savaAdminRole(Long adminId, List<Long> roleIdList) {
        // 删除用户原有的角色关联数据
        adminRoleService.remove(new LambdaQueryWrapper<AdminRole>().eq(AdminRole::getAdminId, adminId));

        // 将角色ID列表转换为AdminRole对象列表
        List<AdminRole> collect = roleIdList.stream().map(roelId -> {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roelId);
            return adminRole;
        }).collect(Collectors.toList());

        // 批量保存用户角色关联数据
        return adminRoleService.saveBatch(collect);
    }
}
